package org.fing.edu.uy.esbadp.requirements;

import java.util.logging.Level;

import org.fing.edu.uy.esbadp.core.constants.EsbAdpPropertyConstants;
import org.fing.edu.uy.esbadp.core.entity.MetaDataConfig;
import org.fing.edu.uy.esbadp.core.entity.PropertiesMonitoring;
import org.fing.edu.uy.esbadp.core.interfaces.IAdpServiceRequirement;
import org.fing.edu.uy.esbadp.logger.LoggerADP;

public class AdpRequirementSimpleCondition implements IAdpServiceRequirement {
	
	private static java.util.logging.Logger logger = LoggerADP.getLogger(AdpRequirementSimpleCondition.class.getSimpleName());
	
	private String operador;
	private String metaName;
	private String propName;
	
	
	public AdpRequirementSimpleCondition(String operador , String metaName , String propName){		
		this.operador = operador;
		this.metaName = metaName;
		this.propName = propName;		
	}
	
	@Override
	public boolean needRequiredAdp(MetaDataConfig metadata,	PropertiesMonitoring properties) {
		double metDouble = 0d;
		double propDouble = 0d;
		
		//busco el valor de la metadata y lo parseo a double
		String metString = metadata.getAttribute(this.metaName);
		if(!(metString==null)){			
			try{
				metDouble = Double.parseDouble(metString);			
			}catch(Exception ex){
				logger.log(Level.SEVERE, "The metadata'" + this.metaName + "' must be parseable to Double.",ex);
				return false;
			}			
		}else{
			logger.log(Level.SEVERE, "The metadata'" + this.metaName + "' is not present.");
			return false;
		}
		
		//busco el valor de la propiedad 
		Object propObj = properties.getPropertyValue(this.propName);
		if(!(propObj==null)){			
			
			if(propObj instanceof Number){
				Number catAux = (Number) propObj ;
				propDouble = catAux.doubleValue();
			}else{
				logger.log(Level.SEVERE, "The property'" + this.propName + "' must be parseable to Double.");
				return false;
			}
			
		}else{
			logger.log(Level.SEVERE, "The property'" + this.propName + "' is not present.");
			return false;
		}	
	
		//determino el resultado de acuerdo al operador.
		if(this.operador.equalsIgnoreCase(EsbAdpPropertyConstants.REQUIREMENTS_ELEMENT_OPERATOR_GREATER)){
			
			return propDouble <= metDouble;
			
		}else if(this.operador.equalsIgnoreCase(EsbAdpPropertyConstants.REQUIREMENTS_ELEMENT_OPERATOR_LESS)){
			
			return propDouble >= metDouble;
			
		}else if(this.operador.equalsIgnoreCase(EsbAdpPropertyConstants.REQUIREMENTS_ELEMENT_OPERATOR_EQUAL)){			
			
			return (propDouble != metDouble);
			
		}else if(this.operador.equalsIgnoreCase(EsbAdpPropertyConstants.REQUIREMENTS_ELEMENT_OPERATOR_DISTINCT)){
			
			return propDouble == metDouble;
			
		}else if(this.operador.equalsIgnoreCase(EsbAdpPropertyConstants.REQUIREMENTS_ELEMENT_OPERATOR_GREATER_EQUAL)){
			
			return propDouble < metDouble;
			
		}else if(this.operador.equalsIgnoreCase(EsbAdpPropertyConstants.REQUIREMENTS_ELEMENT_OPERATOR_LESS_EQUAL)){
			
			return propDouble > metDouble;
			
		}
		
		return false;
	}
	
}
